package com.xlx.openLink.controller;

import com.xlx.openLink.base.BaseDateMessage;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.entity.pojo.Inform;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.service.InformService;
import com.xlx.openLink.vo.RequestInformtLst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author seven
 * @create 2019-10-28 4:23
 **/
@Controller
@RequestMapping("/inform")
@Slf4j
public class InformController {

    @Autowired
    private InformService informService;

    @PostMapping("/listByPage")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestBody RequestInformtLst requestInformtLst) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(informService.listByPage(requestInformtLst));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }

    @PostMapping("/add")
    @ResponseBody
    public BaseDateMessage add(@RequestBody Inform inform) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            informService.add(inform);
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("添加成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("添加失败");
        }
        return baseDateMessage;
    }

    @PostMapping("/option")
    @ResponseBody
    public BaseDateMessage option(@RequestBody Inform inform) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            informService.option(inform);
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("更新成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("更新失败");
        }
        return baseDateMessage;
    }


    @GetMapping ("/selectById")
    @ResponseBody
    public BaseDateMessage selectById(@RequestParam(value = "pkGuid") String pkGuid) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(informService.selectById(pkGuid));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取");
        }
        return baseDateMessage;
    }
}
